package com.entrogy.controller;


import com.alibaba.fastjson.JSON;
import com.entrogy.bean.TokenVo;
import com.entrogy.bean.User1Entity;
import com.entrogy.service.DingRecordSyncService;
import com.entrogy.utils.StrUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.bag.SynchronizedSortedBag;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import com.alibaba.fastjson.JSONObject;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.entrogy.config.Constant.APP_KEY;
import static com.entrogy.config.Constant.APP_SECRET;
import static com.entrogy.config.HttpClientUtils.doGet;
import static com.entrogy.config.HttpClientUtils.doPost;
import static com.entrogy.config.URLConstant.URL_ADDUSER_POST;
import static com.entrogy.config.URLConstant.URL_GET_TOKKEN;


/**
 * @Author 许红松
 * @date 2019/4/26 0026 上午 8:57
 * 钉钉获取考勤记录
 */
@Api(description = "钉钉操作接口")
@RestController
@RequestMapping(value = "/dingRecordSyn")
public class DingRecordSyncController {

     @Autowired
     private DingRecordSyncService dingRecordSyncService;



    @ApiOperation(value = "新增用户")
    @RequestMapping(value = "/insertUser",method = RequestMethod.POST)
    public int insertUser(@ModelAttribute User1Entity user1Entity){
        user1Entity.setDepartment("1,2");
        String url1=URL_GET_TOKKEN+"?appkey="+APP_KEY+"&appsecret="+APP_SECRET;
        JSONObject jsonObject1 = new JSONObject();
        String Token1= doGet(url1,jsonObject1);
        TokenVo tokenVo = JSON.parseObject(Token1,TokenVo.class);
        String token = tokenVo.getAccess_token();

                String url=URL_ADDUSER_POST+"?access_token="+token;
                JSONObject jsonObject = new JSONObject();
                List<Long>  list=new ArrayList<Long>();
                list.add(1L);
                list.add(113708331L);
                jsonObject.put("userid",user1Entity.getUserid());
                jsonObject.put("name",user1Entity.getName());
                jsonObject.put("mobile",user1Entity.getMobile());
                jsonObject.put("department",JSON.toJSONString(list));
                String result= doPost(url,jsonObject);
                System.out.println(result);
        int code=dingRecordSyncService.insertUser1(user1Entity);
        return code;
    }

    @ApiOperation(value = "根据用户获取打卡信息")
    @RequestMapping(value = "/listRecord",method = RequestMethod.POST)
    public String listRecord(@RequestParam(value="userid") String  userid,@RequestParam(value="checkDateFrom")String checkDateFrom,@RequestParam(value="checkDateTo")String checkDateTo,HttpServletRequest request){
        List<String> list=new ArrayList<String>();
        list.add(userid);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("checkDateFrom",checkDateFrom);
        jsonObject.put("checkDateTo",checkDateTo);
        jsonObject.put("userIds",list);
        String url1=URL_GET_TOKKEN+"?appkey="+APP_KEY+"&appsecret="+APP_SECRET;
        JSONObject jsonObject1 = new JSONObject();
        String Token1= doGet(url1,jsonObject1);
        TokenVo tokenVo = JSON.parseObject(Token1,TokenVo.class);
        String token = tokenVo.getAccess_token();
        String dingDingAttendance="https://oapi.dingtalk.com/attendance/listRecord?access_token="+token;
        String Result=doPost(dingDingAttendance,jsonObject);//获取考勤记录
        return Result;
    }


    @ApiOperation(value = "查询钉钉人员信息")
    @RequestMapping(value = "/listUser",method = RequestMethod.POST)
    public String listUser(){
        String url1=URL_GET_TOKKEN+"?appkey="+APP_KEY+"&appsecret="+APP_SECRET;
        JSONObject jsonObject = new JSONObject();
        String Token= doGet(url1,jsonObject);
        TokenVo tokenVo = JSON.parseObject(Token,TokenVo.class);
        String token = tokenVo.getAccess_token();
        String Result="";
        if(token!=null && !"".equals(token)) {
            String dingDing = "https://oapi.dingtalk.com/user/listbypage?access_token="+token+"&department_id=1&offset=0&size=10";
            /*jsonObject.put("offset",0L);
            jsonObject.put("size",10L);*/
            Result = doGet(dingDing, jsonObject);
        }
        return Result;

    }

}



